package com.gitee.feizns.explore.data_structure.sort;

import java.util.Arrays;

/**
 * @author feizns
 * @since 2020/1/2
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] arr = new int[]{ 5, 4, 3, 2, 1 };
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void selectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if ( arr[min] > arr[j] )
                    min = j;
            }
            swap(arr, min, i);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

}
